/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.SRV;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="SRV_ShopServiceList",
            loadOnStartup = 1,
            urlPatterns={"/SRV/ShopServiceList",
                        "/SRV/ShopServiceList/grd_search"})
public class SRV_ShopServiceList extends HttpServlet {
   
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/Srv/ShopServiceList")) {
            userPath = "/Srv/ShopServiceList";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();


        if (userPath.equals("/SRV/ShopServiceList/grd_search")) {
            response.setContentType("text/json;charset=utf-8");
//            String qstr_page = request.getParameter("page"); // get the requested page
//            String qstr_limit = request.getParameter("rows"); // get how many rows we want to have into the grid
//            String qstr_sidx = request.getParameter("sidx"); // get index row - i.e. user click to sort
//            String qstr_sord = request.getParameter("sord"); // get the direction
//
//            if (qstr_sidx.isEmpty()) {
//                qstr_sidx = "id";
//                qstr_sord = "asc";
//            }
//
//            String txtName = (request.getParameter("txtName") != null) ? request.getParameter("txtName") : "";
//            String txtPrice = (request.getParameter("txtPrice") != null) ? request.getParameter("txtPrice") : "";
//            String txtCurrency = (request.getParameter("txtCurrency") != null) ? request.getParameter("txtCurrency") : "";
//            String txtDoctorName = (request.getParameter("txtDoctorName") != null) ? request.getParameter("txtDoctorName") : "";

            PrintWriter out = response.getWriter();
//            List<SrvCureservice> lSrvCureservice;
//            List rows;
//            JQGridRow row;
//            List cells;
//            String strJson = "";
//            int totalRecords = 0;
//            int endList = Integer.parseInt(qstr_limit)*Integer.parseInt(qstr_page);
//            int startList = Integer.parseInt(qstr_limit)*Integer.parseInt(qstr_page) - Integer.parseInt(qstr_limit);
            try {
//                //Query Java Object
//                lSrvCureservice = srvCureserviceFacade.LoadDataService(txtName, txtPrice, txtCurrency, txtDoctorName, qstr_sidx, qstr_sord);
//                totalRecords = lSrvCureservice.size();
//                if (endList>totalRecords){
//                    endList=totalRecords;
//                }
//                //Use List rows
//                rows = new ArrayList();
//                if (!lSrvCureservice.isEmpty()) {
//                    for (SrvCureservice obj : lSrvCureservice.subList(startList, endList)) {
//                        row = new JQGridRow();
//                        row.setId(obj.getId());
//
//                        cells = new ArrayList();
//                        cells.add(obj.getId().toString());
//                        cells.add(obj.getName());
//                        cells.add(obj.getPrice().toString());
//                        cells.add(obj.getCurrency());
//                        if (obj.getMDoctor() != null) {
//                            cells.add(obj.getMDoctor().getName() + " " + obj.getMDoctor().getSurname());
//                        } else {
//                            cells.add("");
//                        }
//                        if (obj.getMHospital() != null) {
//                            cells.add(obj.getMHospital().getName());
//                        } else {
//                            cells.add("");
//                        }
//
//
//                        row.setCell(cells);
//                        rows.add(row);
//                    }
//                }
                String strJson = "{\"page\":1,\"total\":1,\"records\":17,\"rows\":[{\"id\":1,\"cell\":[\"1\",\"OSIM uDivine \",\"169900.0000\",\"Baht\",\"Goods\",\"Inspiring Well-being\"]},{\"id\":2,\"cell\":[\"2\",\"OSIM uCrown2\",\"8990.0000\",\"Baht\",\"Goods\",\"OSIM Inspiring Well-being\"]},{\"id\":3,\"cell\":[\"3\",\"OSIM uKimono Mini\",\"3990.0000\",\"Baht\",\"Goods\",\"OSIM Inspiring Well-being\"]},{\"id\":4,\"cell\":[\"4\",\"OSIM iCheck 100\",\"3980.0000\",\"Baht\",\"Goods\",\"OSIM Inspiring Well-being\"]},{\"id\":5,\"cell\":[\"5\",\"Minerva Fill me up\",\"33000.0000\",\"Baht\",\"Service\",\"MINERVA CLINIC\"]},{\"id\":6,\"cell\":[\"6\",\"Biopeel\",\"2000.0000\",\"Baht\",\"Service\",\"Prime'Clinic\"]},{\"id\":7,\"cell\":[\"7\",\"Face bright\",\"15200.0000\",\"Baht\",\"Service\",\"Prime'Clinic\"]},{\"id\":8,\"cell\":[\"8\",\"Medical tourism 2days 1 night\",\"4990.0000\",\"Baht\",\"Program\",\"Wanathara Hotel and Resort Co.,Ltd.\"]}]}";
                out.println(strJson);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            }

            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }
}
